package personal.xjl._23java1studentdemo.controller;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import personal.xjl._23java1studentdemo.service.AdminService;
import personal.xjl._23java1studentdemo.service.StudentService;
import personal.xjl._23java1studentdemo.service.TeacherService;


@Controller
public class LoginController {
    @Autowired
    StudentService studentService;
    @Autowired
    TeacherService teacherService;
    @Autowired
    AdminService adminService;
    @RequestMapping("loginValidate")
    public String loginValidate(int role,String username, String password, Model model, HttpSession session) {
        switch (role) {
            case 1://如果登录成功，转向到listStudents.html页面，否则停留在登录页面
            { if(adminService.login(username,password)) {
                //加一个session有效的变量，令牌，拦截器的通关检查
                session.setAttribute("username", username);
                return "redirect:/Course/list";
            }
            else
                return "redirect:/login";}
            case 2:
            {if(teacherService.login(username,password)) {
                //加一个session有效的变量，令牌，拦截器的通关检查
                session.setAttribute("username", username);
                return "redirect:/Teacher/list";
            }
            else
                return "redirect:/login";}
            case 3:    //如果登录成功，转向到listStudents.html页面，否则停留在登录页面
            { if(studentService.login(username,password)) {
                //加一个session有效的变量，令牌，拦截器的通关检查
                session.setAttribute("username", username);
                return "redirect:/Student/list/1/3";
            }
            else
                return "redirect:/login";}
            default: return "redirect:/login";
        }

    }
    @RequestMapping("/login")
    public String login(){
        return "login";
    }
    @RequestMapping("/logout")
    public String logout(HttpSession session){
        //让当前会话失效，当前用户失去令牌,回到登录页面
        session.invalidate();
        return "redirect:/login";
    }
}
